function followBind() {
    var myTableBody= $(".myWords-my-table>tbody");
    myTableBody.delegate(".glyphicon-edit","click",function(){
        Aid = $(this).parent().parent().data("addid");
        getFollowWords();
    });
}
function getFollow() {
    var username = $("#username").data("username");

    $.post("/ajax/getFollow", {username: username}, function (json) {
        var res = $.parseJSON(json);
        var total = res.total;
        var follows = res.follows;
        var followApp;
        var myTableBody= $(".myWords-my-table>tbody");
        myTableBody.html("");
        for (i = 0; i < total; i++) {
            followApp = $('<tr><td>'+follows[i].name+'</td><td><span class="glyphicon glyphicon-edit"></td></tr>');
            followApp.data("addid",follows[i].AID);
            myTableBody.append(followApp);
        }
    });
}
function followWordsBind(){
    var wordsTableBody = $(".mywords-follow-words-table>tbody");
    wordsTableBody.delegate(".glyphicon-remove","click",function(){
        var tr = $(this).parent().parent();
        var LID = tr.data("lid");
        $.post("/ajax/deleteFollowMywords",{AID:Aid,LID:LID},function(data){
            if(data === "OK"){
                tr.remove();
            }
        });
    });
}
function getFollowWords() {
    $.post("/ajax/getFollowMyWords",{AID:Aid},function (json) {
        var res = $.parseJSON(json);
        var total = res.total;
        var words = res.words;
        var wordsTableBody = $(".mywords-follow-words-table>tbody");
        wordsTableBody.html("");
        for (i = 0; i < total; i++) {
            followApp = $('<tr><td>'+words[i].lable+'</td><td><span class="glyphicon glyphicon-remove"></span></td></tr>');
            followApp.data("lid",words[i].LID);
            wordsTableBody.append(followApp);
        }
    });
}

function myWordsBind(){
    var wordsTableBody = $(".mywords-my-words-table>tbody");
    $("#myWords-add").click(function(){
        layer.prompt({
            formType: 0,
            value: '',
            title: '添加分词'
        }, function(value, index, elem){
            $.post("/ajax/addMyWords",{text:value},function (data) {
                if(data === "OK"){
                    getMyWords();
                }
            });
            layer.close(index);
        });
    });
    wordsTableBody.delegate(".glyphicon-plus","click",function(){
        if(Aid === 0){
            return;
        }
        var tr = $(this).parent().parent();
        var LID = tr.data("lid");
        $.post("/ajax/addToFollowWords",{AID:Aid,LID:LID},function(data){
            if(data === "OK"){
                getFollowWords();
            }
        });
    });
    wordsTableBody.delegate(".glyphicon-remove","click",function(data){
        var tr = $(this).parent().parent();
        var LID = tr.data("lid");
        $.post("/ajax/deleteMywords",{LID:LID},function(data){
            if(data === "OK"){
                tr.remove();
                getFollowWords();
            }
        });
    });
}

function getMyWords(){
    $.post("/ajax/getMyWords",function (json) {
        var res = $.parseJSON(json);
        var total = res.total;
        var words = res.words;
        var wordsTableBody = $(".mywords-my-words-table>tbody");
        wordsTableBody.html("");
        for (i = 0; i < total; i++) {
            followApp = $('<tr><td>'+words[i].lable+'</td><td><span class="glyphicon glyphicon-plus"></span><span class="glyphicon glyphicon-remove"></span></td></tr>');
            followApp.data("lid",words[i].LID);
            wordsTableBody.append(followApp);
        }
    });
}
function init() {
    Aid = 0;
}
function bind(){
    followBind();
    followWordsBind();
    myWordsBind()
}
$(function(){
    init();
    getFollow();
    bind();
    getMyWords()
});